<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2019/2/13
 * Time: 11:18
 */
namespace App\Ordersys\Controllers\Admin;
use App\Base\Contracts\AreaInterface;
use App\Base\Controllers\Controller;

use App\Ordersys\Contracts\OrdersysCustomerInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CustomerController extends Controller
{

    /**
     * 产品接口
     * @return OrdersysCustomerInterface
     */
    public function getOrdersysCustomerInterface()
    {
        return app()->make(OrdersysCustomerInterface::class);
    }

    /**
     * 请选择省区
     * @return AreaInterface
     */
    public function getAreaInterface(){
        return app()->make(AreaInterface::class);
    }
    /**
     * 广告首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                $truename=$request->input("truename");
                if(!empty($truename)){
                    $query->where("truename","like",'%'.$truename.'%');
                }
                $mobile=$request->input("mobile");
                if(!empty($mobile)){
                    $query->where("mobile","like",'%'.$mobile.'%');
                }
            };
            return  $this->getOrdersysCustomerInterface()->getList($where);
        }
        return view("ordersys.customer.index");
    }
    public function manageIndex(Request $request){
        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                $truename=$request->input("truename");
                if(!empty($truename)){
                    $query->where("truename","like",'%'.$truename.'%');
                }
                $mobile=$request->input("mobile");
                if(!empty($mobile)){
                    $query->where("mobile","like",'%'.$mobile.'%');
                }
            };
            return  $this->getOrdersysCustomerInterface()->getList($where);
        }
        return view("ordersys.customer.manage_index");

    }

    public function manageSys(Request $request){
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
                'money' => 'required',
                'option_type'=>'required',
            ],["id.required"=>'id必须有值',"money.required"=>'金额是必须',"option_type.required"=>'操作类型不能为空']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getOrdersysCustomerInterface()->saveOptionMoneyData($request->all());
        }
        $data=$this->getOrdersysCustomerInterface()->getInfo($id);
        return view("ordersys.customer.manage_sys")->with("data",$data);
    }

    public function manageScore(Request $request){
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
                'money' => 'required',
                'option_type'=>'required',
            ],["id.required"=>'id必须有值',"money.required"=>'金额是必须',"option_type.required"=>'操作类型不能为空']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getOrdersysCustomerInterface()->saveOptionScoreData($request->all());
        }
        $data=$this->getOrdersysCustomerInterface()->getInfo($id);
        return view("ordersys.customer.manage_score")->with("data",$data);
    }

    /**
     * 余额日志
     * @param Request $request
     */
    public function manageLog(Request $request){
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            return  $this->getOrdersysCustomerInterface()->getUserBalanceLog($id);
        }
        return view("ordersys.customer.manage_log")->with("id",$id);
    }

    public function manageScoreLog(Request $request){
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            return  $this->getOrdersysCustomerInterface()->manageScoreLog($id);
        }
    }





    /**
     * 明细
     */
    public function manageDetail(Request $request){
        $id=$request->input("id");
        return view("ordersys.customer.manage_detail")->with("id",$id);
    }

    /**
     * 广告编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function edit(Request $request)
    {
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'mobile' => 'required',
                'truename' => 'required',
            ],["mobile.required"=>'手机号必须有值',"truename.required"=>'真实名必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getOrdersysCustomerInterface()->saveData($request->all());
        }
        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);  //区域
        $data=$this->getOrdersysCustomerInterface()->getInfo($id);
        $data->test=empty($data->brithday)?date("Y-m-d"):date("Y-m-d H:i:s",$data->brithday);
        return view("ordersys.customer.edit")->with("areaList",$areaList)->with("data",$data);
    }

    /**
     * 广告添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {

        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'mobile' => 'required',
                'truename' => 'required',
            ],["mobile.required"=>'手机号必须有值',"truename.required"=>'真实姓名必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getOrdersysCustomerInterface()->saveData($request->all());
        }
        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);  //区域
        return view("ordersys.customer.add")->with("areaList",$areaList);
    }

    /**
     * 会员列表
     * @param Request $request
     */
    public function userList(Request $request){
        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                $truename=$request->input("truename");
                if(!empty($truename)){
                    $query->where("truename","like",'%'.$truename.'%');
                }
                $mobile=$request->input("mobile");
                if(!empty($mobile)){
                    $query->where("mobile","like",'%'.$mobile.'%');
                }
            };
            return  $this->getOrdersysCustomerInterface()->getList($where);
        }
        return view("ordersys.customer.user_list");
    }

    /**
     * 广告删除
     * @param Request $request
     */
    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
            };
            return  $this->getOrdersysCustomerInterface()->deleteData($where);
        }
    }

    /**
     * 改变状态
     * @param Request $request
     */
    public function changeStatus(Request $request){
        return  $this->getOrdersysCustomerInterface()->saveData($request->all());
    }

}
